package widgets
{
	import com.bit101.components.PushButton;
	import com.bit101.components.RadioButton;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import globals.TrueLoop;
	
	import padPack.PadPushButton;
	import padPack.TruePad;
	
	import utils.Button;
	import utils.Checkbutton;
	import utils.Label;
	import utils.LabelInput;
	import utils.LabelInputNumber;
	import utils.Seperator;
	import utils.Slider;

	public class LoopSettings extends Sprite
	{
		public static const ADD_PRIMITIVE_BTN_CLICK:String = "addPrimitiveButtonClick";
		
		private var _truePad:TruePad;
		
		private var _container1:Holder;
		private var _container2:Holder;
		private var _container3:Holder;
		
		private var _loop:TrueLoop;
		
		private var _holder:Holder;
		private var _nameLabel:LabelInput;
		private var _lengthLabel:LabelInputNumber;
		private var _volume:Slider;
		
		private var _playFull:RadioButton;
		private var _oneshot:RadioButton;
		private var _hold:RadioButton;
		
		private var _addPrimitiveButton:PushButton;
		private var _editModeBtn:PushButton;
		
		public function LoopSettings(truePad:TruePad)
		{
			_truePad = truePad;
			_container1 = new Holder();
			_container2 = new Holder();
			_container3 = new Holder();
			_container1.visible = false;
			_container2.visible = false;
			
			_holder = new Holder();
			_nameLabel = new LabelInput("");
			_nameLabel.addEventListener(LabelInput.CHANGE_EVENT, nameChange);
			_lengthLabel = new LabelInputNumber(_loop == null ? "0" : _loop.length.toString());
			_lengthLabel.addEventListener(LabelInput.CHANGE_EVENT, lengthChange);
			_volume = new Slider("Volume", 100, 100, 150);
			_volume.addEventListener(Slider.SLIDE_CHANGE, volumeChange);
			//--
			var seperator:Seperator = new Seperator(Seperator.HORIZONTAL, 235);
			var recordLabel:Label = new Label("Record");
			_playFull = new RadioButton(null, 0, 0, "Hold", true, pushModeChange);
			_playFull.groupName = "pushMode";
			_oneshot = new RadioButton(null, 0, 0, "Oneshot", false, pushModeChange);
			_oneshot.groupName = "pushMode";
			_hold = new RadioButton(null, 0, 0, "Trigger", false, pushModeChange);
			_hold.groupName = "pushMode";
			
			//_oneshot.addEventListener(Checkbutton.EVENT, oneshotTrigger);
			_addPrimitiveButton = new PushButton(null, 0, 0, "Add sound");
			_addPrimitiveButton.addEventListener(MouseEvent.CLICK, addPrimitiveButtonClick);
			_editModeBtn = new PushButton(null, 0, 0, "Edit mode");
			_editModeBtn.toggle = true;
			_editModeBtn.addEventListener(MouseEvent.CLICK, editModeToggle);
			//--
			var seperator2:Seperator = new Seperator(Seperator.HORIZONTAL, 235);
			var globalLabel:Label = new Label("Global");
			
			_container1.addWidget(_nameLabel, 15);
			_container1.addWidget(_lengthLabel, 15);
			_container1.addWidget(_volume, 15);
			
			_container1.addWidget(seperator, 1);
			//--
			_container2.addWidget(recordLabel, 15);
			_container2.addWidget(_playFull, 10);
			_container2.addWidget(_oneshot, 10);
			//_container2.addWidget(_hold, 10);
			_container2.addWidget(seperator2, 1);
			//--
			_container3.addWidget(globalLabel, 15);
			_container3.addWidget(_addPrimitiveButton, 15);
			_container3.addWidget(_editModeBtn, 15);
			
			_holder.addWidget(_container1, _container1.height);
			_holder.addWidget(_container2, _container2.height);
			_holder.addWidget(_container3, _container3.height);
			
			addChild(_holder);
		}
		
		private function pushModeChange(event:Event):void
		{
			_loop.oneShot = _oneshot.selected;
		}
		
		public function get loop():TrueLoop
		{
			return _loop;
		}

		public function set loop(value:TrueLoop):void
		{
			if (_loop != null && _loop.hasEventListener(TrueLoop.DATA_REFRESH))
			{
				_loop.removeEventListener(TrueLoop.DATA_REFRESH, dataRefresh);
			}			
			_loop = value;
			_nameLabel.text = _loop.name;
			_lengthLabel.text = _loop.length.toString();
			_volume.setValue(_loop.volume * 100);
			if (_loop.oneShot) {
				_oneshot.selected = true;
			} else {
				_playFull.selected = true;
			}
			_container1.visible = true;
			_container2.visible = true;
			_loop.addEventListener(TrueLoop.DATA_REFRESH, dataRefresh);
		}
		
		protected function volumeChange(event:Event):void
		{
			_loop.volume = Number(_volume.value/100);
		}
		
		protected function addPrimitiveButtonClick(event:MouseEvent):void
		{
			dispatchEvent(new Event(ADD_PRIMITIVE_BTN_CLICK));
		}
		
		protected function nameChange(event:Event):void
		{
			_loop.name = _nameLabel.text;
		}
		
		protected function lengthChange(event:Event):void
		{
			_loop.setLastPoint(int(_lengthLabel.text));
		}
		
		protected function dataRefresh(event:Event):void
		{
			_lengthLabel.text = (_loop.length).toString();
		}
		
		protected function editModeToggle(event:MouseEvent):void
		{
			for each (var btn:PadPushButton in _truePad.pushButtons) {
				btn.setEditMode(_editModeBtn.selected);
			}
		}
		
	}
}